/*
 * @version: 
 * @Author: yu
 * @Date: 2023-07-01 14:54:12
 * @LastEditors: Andy
 * @LastEditTime: 2023-07-05 10:33:53
 */
import { specsList, specsCount } from '@/request/api'

//声明state
const state = {
    list: [],  //规格属性
    page: 1,  //页码数
    size: 3,   //查询条数
    total: 0, //总计录数
}



//修改state
const mutations = {
    changeList(state, arr) {
        state.list = arr
    },
    changeTotal(state, total) {
        state.total = total
    },
    changePage(state, page) {
        state.page = page
    }

}


const actions = {
    listActions(context) {
        let params = {
            page: context.state.page,
            size: context.state.size
        }
        //发起请求规格
        specsList(params).then(res => {
            // console.log(res);
            if (res.code === 200) {
                if (res.list === null || res.list.length === 0) {
                    //当前页码数减一
                    let page = context.state.page - 1;
                    context.commit('changePage', page)
                    //再次更新  自调
                    context.dispatch('listActions');
                    return
                }
                context.commit('changeList', res.list)
            }

        })
    },
    countActions(context) {
        //发起请求
        specsCount().then(res => {
            // console.log(res);
            if (res.code === 200) {
                context.commit('changeTotal', res.list[0].total)
            }
        })
    },
    pageActions(context, page) {
        context.commit('changePage', page)
    }
}


//派生状态
const getters = {
    list(state) {
        return state.list
    },
    total(state) {
        return state.total
    },
    size(state) {
        return state.size
    }
}


export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}